<template>
  <div class="container">
    <div class="takeCash-page">
      <div class="wrapper">
        <div class="header">
          <div class="left">到零钱</div>
          <div class="right">
            <div class="bank">{{userInfo.nickName}}</div>
            <div class="tips">马上到账(如有问题,请联系管理员)</div>
          </div>
        </div>
        <div class="body">
          <div class="title">提现金额</div>
          <div class="content">
            <div class="icon">¥</div>
            <div class="input-box">
              <input class="input" v-model="getMoney" type="digit" @input="handleInput">
            </div>
          </div>
          <div class="balance">
            <div class="text">账户余额¥{{userInfo.money}}元，</div>
            <div class="pull" @click="getAllMoney">全部提现</div>
          </div>
          <div class="button" :class="{ 'on': !isDisabled }" @click="submit">提现</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      isDisabled: true,
      userInfo: {},

      getMoney: ''
    }
  },

  onShareAppMessage (res) {
    let self = this;
    let url = `${self.$confs.shareUrl}?id=${self.userInfo.id}`;
    return {
      title: self.$confs.shareTitle,
      path: url,
    }
  },

  computed: {
    res () {
      return this.$store.state.res;
    },
  },

  methods: {
    getAllMoney(){
      this.getMoney = this.userInfo.money;
      this.handleInput();
    },
    handleInput () {
      if (this.getMoney && !isNaN(this.getMoney)) {
        this.isDisabled = false;
      }
      else {
        this.isDisabled = true;
      }
    },

    submit(){
      let self = this;
      if (self.isDisabled) return;

      if (!self.getMoney || self.getMoney > self.userInfo.money || self.getMoney < 1) {
        self.$modal.show('提现金额必须大于 1 , 并且不超过余额!');
        return ;
      }
      self.isDisabled = true;
      self.$modal.show({
        content: `提现金额${self.getMoney}元，是否确认提现？`,
        success: (res) => {
          if (res.confirm) {
            self.$toast.show();
            self.$axios.getEnchashment({
              money : self.getMoney
            }).then(res=>{
              self.$toast.hide();
              self.isDisabled = false;
              if (0 === res.data.code * 1) {
                if (res.data.data.payResult.result_code === 'SUCCESS') {
                  self.$modal.show('提现成功!');
                  self.getMoney = '';

                  // console.log(res.data.data.member);
                  wx.setStorageSync('userInfo' , res.data.data.member);
                  self.userInfo = res.data.data.member;

                }else {
                  self.$modal.show('提现失败!');
                }

              }else {
                self.$modal.show('提现失败!');
              }
            })
          }
        }
      })
    },

    getUserInfo (userInfo) {
      let self = this;
      let params = {
        code: wx.getStorageSync('code'),
        avatarUrl: userInfo.avatarUrl,
        nickName: userInfo.nickName,
      };
      this.$axios.login(params).then((res) => {
        self.$toast.hide();
        if (res.data.code * 1 === 0) {
          wx.setStorageSync('userInfo', res.data.data.member);
          wx.setStorageSync('token', res.data.data.token);
        }
      });
    },

    init(){
      let self = this;
      self.userInfo = wx.getStorageSync('userInfo');
    }
  },

  mounted () {
    this.init();
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
